home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / BMP2BGI1.ZIP / BMP2BGI2.C < prev    next >
C/C++ Source or Header  |  1991-05-25  |  8KB  |  183 lines

  1. //////////////////////////////////////////////////////////////////////////
  2. // ProcessBMP(char *Filename)
  3. //             This routine opens the BMP file and displays it pixel by
  4. //             pixel.  Then, the video image is translated to BGI format.
  5. //             Call AccessBMP before using this routine.  BMP file 
  6. //             structures should be defined as global vars in the calling 
  7. //             routine.  After translating, the BGI-formatted image is 
  8. //             stored in the file w/ a .BGI extension.  If the FILE.BGI 
  9. //             already exists, it is written over.
  10. //////////////////////////////////////////////////////////////////////////
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <graphics.h>
  16. #include <alloc.h>
  17. #include "bmp.h"                                // BMP file structure
  18. #include "bgi.h"                                // BGI file structure
  19.  
  20.                                                 // Max size of 10k.  Borland's
  21. #define VBUFFSIZE 10240                         // default size (4k) is too small for 
  22.                                                 // practical purposes.
  23.  
  24. extern BITMAPFILEHEADER BMPHeader;              // BMP File header
  25. extern BITMAPINFOHEADER BMPInfo;
  26.  
  27. extern void StoreBGI(FILE *, int, int, char far *VidBuf[5]);
  28.  
  29.  
  30. BGIPREFACE BGIHeader;                           // BGI File Header
  31.  
  32.                                                 // 16-Color pallete for Microsoft's BMP's
  33. struct palettetype pal = { {MAXCOLORS},         // which is different from Borland's 16-
  34.                            { EGA_BLACK,         // color pallete, intentionally?
  35.                              EGA_RED,
  36.                              EGA_GREEN,
  37.                              EGA_CYAN,
  38.                              EGA_BLUE,  
  39.                              EGA_MAGENTA,
  40.                              EGA_BROWN,
  41.                              EGA_DARKGRAY,
  42.                              EGA_LIGHTGRAY,
  43.                              EGA_LIGHTRED,
  44.                              EGA_LIGHTGREEN,
  45.                              EGA_YELLOW,
  46.                              EGA_LIGHTBLUE,
  47.                              EGA_LIGHTMAGENTA,
  48.                              EGA_LIGHTCYAN,
  49.                              EGA_WHITE
  50.                             }
  51. };
  52.  
  53.  
  54.         
  55.  
  56. void ProcessBMP(char *BMPFile, char far *VidBuf[5])
  57. {
  58.         FILE *Fn, *Fo;                          // Fn: BMP file; Fo: BGI file
  59.         int gdriver=DETECT,gmode;               // vars for graphics mode
  60.         int x, y, maxx, maxy;                   // bookkeeping vars
  61.         char Color, buff[80];                   
  62.  
  63.  
  64.         Fn = fopen(BMPFile,"rb");               // Open BMP file
  65.  
  66.         strset(buff,NUL);                       // Create BGI file name as
  67.         strtok(BMPFile,".");                    // "BMP_File_Name.BGI"
  68.         sprintf(buff,"%s.BGI",BMPFile);
  69.         Fo = fopen(buff,"wb");
  70.                                                 // Jump to image data in 
  71.         fseek(Fn,BMPHeader.bfOffBits,SEEK_SET); // BMP file
  72.                                                 
  73.  
  74.                                                 // register a driver that was added into graphics.lib 
  75.                                                 // & adjust the size
  76.         registerfarbgidriver(EGAVGA_driver_far);
  77.         setgraphbufsize(VBUFFSIZE);
  78.         
  79.                                                 // check heap space
  80.         if ((BMPInfo.biWidth * BMPInfo.biHeight) >= farcoreleft())
  81.         {
  82.                 printf("Coreleft> not enough heap space. %lu bytes were requested\n", (unsigned long) BMPInfo.biWidth * BMPInfo.biHeight);
  83.                 printf("          There are %lu bytes of available RAM.\n", farcoreleft());
  84.                 printf("          Try reducing the size of the image.\n");
  85.                 printf("Coreleft> Press any key to halt.");
  86.                 getch();
  87.                 exit(1);
  88.         }
  89.                                                 // initialize graphics and check status 
  90.         initgraph(&gdriver, &gmode, "");
  91.         x = graphresult();
  92.         if (x != grOk)                          // an error occurred 
  93.         {
  94.                 printf("InitGraph> %s\n", grapherrormsg(x));
  95.                 printf("InitGraph> Press any key to halt.");
  96.                 getch();
  97.                 exit(1); 
  98.         }
  99.  
  100.                                                 // Set the image size based the smaller
  101.                                                 // of the video screen or video image
  102.         x = getmaxx();
  103.         y = getmaxy();
  104.         maxx = min(x,BMPInfo.biWidth);
  105.         maxy = min(y,BMPInfo.biHeight);
  106.         
  107.  
  108.  
  109.                                                 // Set pallette to accomodate
  110.                                                 // Microsoft's 16-color pallete
  111.         setallpalette(&pal);                
  112.    
  113.                                                 // Inform User what's happening
  114.         x = 1;
  115.         cleardevice();
  116.         
  117.         outtextxy(1,x,"First, the BMP will be displayed...this will take some time.");
  118.         x += 7 + textheight("W");
  119.         outtextxy(1,x,"Then, the image will be slowly masked...this will also take time.");
  120.         x += 27 + textheight("W");
  121.         outtextxy(1,x,"* Press Any Key When Ready *");
  122.         getch();
  123.         cleardevice();
  124.                                                 // Full-screen images take a long time,
  125.                                                 // so warn the user of this
  126.         if (maxx == getmaxx() && maxy == getmaxy()) outtextxy(1,1,"Now would be a good time to make some coffee!");
  127.  
  128.  
  129. ////////////////////////////////////////////////// The heart of it all 
  130.  
  131.         y = maxy;
  132.                                                 // Read in & display BMP
  133.                                                 // Note: BMP's are stored in
  134.                                                 // bottom-up fashion, so we'll
  135.                                                 // draw from the bottom to the top
  136.                                                 // Also, since the program is setup
  137.                                                 // only for 16-Color BMP's, there
  138.                                                 // are 4 bits for each pixel
  139.         do
  140.         {
  141.                 for (x=0; x < maxx; x++)
  142.                 {
  143.                         fread(&Color,sizeof(Color),1,Fn);
  144.                         putpixel(x++,y,(Color>>4));
  145.                         putpixel(x,y,(Color&0xf));
  146.                 }
  147.          } while (y-- > 0);
  148.                                                 // Set up BGI Header
  149.          BGIHeader.PType = 1;
  150.          BGIHeader.palette = pal;
  151.          BGIHeader.Width  = maxx;
  152.          BGIHeader.Height = maxy;
  153.                                                 // Figure out where in BGI
  154.                                                 // file the image data will
  155.                                                 // be placed, so that the
  156.                                                 // BGI Header can preceed this
  157.          fseek(Fo, (long) sizeof(BGIHeader), SEEK_SET);
  158.          BGIHeader.Foffset = ftell(Fo);
  159.                                                 // Now jump back to start of BGI
  160.                                                 // file and write out BGI Header
  161.          fseek(Fo, 0L, SEEK_SET);
  162.          fwrite(&BGIHeader,sizeof(BGIHeader), 1, Fo);
  163.  
  164.  
  165.                                                 // Save video image into video buffer
  166.          StoreBGI(Fo, BGIHeader.Width, BGIHeader.Height,VidBuf);
  167.  
  168.          y = 0;
  169.                                                 // Now erase the image to show
  170.                                                 // user that something is happening
  171.          do
  172.          {
  173.                 for (x=0; x < maxx; x++) putpixel(x,y,EGA_BLACK);
  174.          } while (y++ < maxy);
  175.  
  176.  
  177.          closegraph();
  178.  
  179.          fclose(Fo);
  180.          fclose(Fn);
  181. }
  182.  
  183.